<template>
  <div class="register">
    <div class="register-left">
      <div class="app-logo">
        <img src="@/assets/logo/logo.png" alt="logo" />
        <h1>求知若饥 · 虚心若愚</h1>
      </div>
      <div class="my-auto">
        <img src="@/assets/images/login-box-bg.svg" alt="" />
        <div class="app-box">
          <span>开箱即用的中后台管理系统</span>
        </div>
        <div class="app-desc">输入您的个人详细信息开始使用！</div>
      </div>
    </div>
    <div class="register-right">
      <div class="register-box">
        <el-form ref="registerRef" :model="registerForm" :rules="registerRules" class="register-form">
          <h3 class="title">注册</h3>

          <el-form-item prop="username">
            <el-input v-model="registerForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
              <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              size="large"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter="handleRegister"
            >
              <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              size="large"
              auto-complete="off"
              placeholder="确认密码"
              @keyup.enter="handleRegister"
            >
              <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
            </el-input>
          </el-form-item>

          <el-form-item style="width:100%;">
            <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleRegister">
              <span v-if="!loading">注 册</span>
              <span v-else>注 册 中...</span>
            </el-button>
            <div style="float: right;">
              <span class="link-type" @click="handleLogin">使用已有账户登录</span>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { register } from '@/api/login';
import { RegisterForm, TenantVO } from '@/api/types';
import { to } from 'await-to-js';
import { encrypt } from "@/utils/jsencrypt";

const router = useRouter();

const registerForm = ref<RegisterForm>({
  username: "",
  password: "",
  confirmPassword: "",
  userType: "sys_user"
});


// 验证两次输入的密码是否一致
const equalToPassword = (rule: any, value: string, callback: any) => {
  if (registerForm.value.password !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};

const registerRules: ElFormRules = {
  username: [
    { required: true, trigger: "blur", message: "请输入您的账号" },
    { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" },
    { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" }
  ],
  confirmPassword: [
    { required: true, trigger: "blur", message: "请再次输入您的密码" },
    { required: true, validator: equalToPassword, trigger: "blur" }
  ],
};

const loading = ref(false);
const registerRef = ref<ElFormInstance>();

const handleRegister = () => {
  registerRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true;
      //RSA密码加密传输
      registerForm.value.password = encrypt(registerForm.value.password as string) as string
      registerForm.value.confirmPassword = encrypt(registerForm.value.confirmPassword as string) as string
      const [err] = await to(register(registerForm.value));
      if (!err) {
        const username = registerForm.value.username;
        await ElMessageBox.alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", "系统提示", {
          dangerouslyUseHTMLString: true,
          type: "success",
        });
        loading.value = false;
        router.push('login')
      } else {
        loading.value = false;
      }
    }
  });
}


const handleLogin = () => {
  router.push('/login')
}
</script>

<style lang="scss" scoped>
.register {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
  padding: 1.5rem 5rem;
  box-sizing: border-box;

  &::before {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    margin-left: -48%;
    background-image: url(http://static-vben.yudao.iocoder.cn/assets/login-bg-b9f5c736.svg);
    background-position: 100%;
    background-repeat: no-repeat;
    background-size: auto 100%;
    content: "";
    z-index: -1;
  }

  .register-left {
    width: 50%;
    height: 100%;

    .app-logo {
      display: flex;
      width: 60%;
      height: 80px;
      position: absolute;
      top: 12px;
      align-items: center;
      padding-left: 7px;
      cursor: pointer;
      transition: all .2s ease;

      img {
        width: 48px;
      }

      h1 {
        font-size: 24px;
        color: #fff;
        font-weight: 700;
        transition: all .5s;
        line-height: normal;
        margin-left: 1rem;
      }
    }

    .my-auto {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      img {
        width: 50%;
      }

      .app-box {
        margin-top: 3rem;

        span {
          color: #fff;
          font-size: 1.875rem;
          line-height: 2.25rem;
        }
      }

      .app-desc {
        color: #fff;
        font-size: 14px;
        margin-top: 2rem;
      }
    }
  }

  .register-right {
    width: 50%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .title {
      font-size: 24px;
      font-weight: bold;
      margin: 0px auto 30px auto;
      text-align: center;
      color: #707070;
    }

    .register-form {
      background: #ffffff;
      width: 400px;
      height: 370px;
      padding: 25px 25px 5px 25px;

      .el-input {
        height: 40px;

        input {
          height: 40px;
        }
      }

      .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 0px;
      }
    }

    .register-tip {
      font-size: 13px;
      text-align: center;
      color: #bfbfbf;
    }

    .register-code {
      width: 33%;
      height: 40px;
      float: right;

      img {
        cursor: pointer;
        vertical-align: middle;
      }
    }

    .el-register-footer {
      height: 40px;
      line-height: 40px;
      position: fixed;
      bottom: 0;
      width: 100%;
      text-align: center;
      color: #fff;
      font-family: Arial, serif;
      font-size: 12px;
      letter-spacing: 1px;
    }

  }
}
</style>
